package edu.wit.common.utils;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.io.*;

public class MarkUpParser extends HTMLEditorKit.ParserCallback {
    private static MarkUpParser html2Text = new MarkUpParser();
    StringBuffer stringBuffer;
    private MarkUpParser() {
    }
    public void parse(String str) throws IOException {
        InputStream iin = new ByteArrayInputStream(str.getBytes());
        Reader in = new InputStreamReader(iin);
        stringBuffer = new StringBuffer();
        ParserDelegator delegator = new ParserDelegator();
        delegator.parse(in, this, Boolean.TRUE);
        iin.close();
        in.close();
    }
    public void handleText(char[] text, int pos) {
        stringBuffer.append(text);
    }
    public String getText() {
        return stringBuffer.toString();
    }
    public static String getContent(String str) {
        try {
            html2Text.parse(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html2Text.getText();
    }
//    public static void main(String[] args) {
//        String text = MarkUpParser.getContent("<p>12555545656<strong>发发发<em>的负担<span style=\"text-decoration: underline;\">和各个环节<span style=\"text-decoration: line-through;\">1424523</span></span></em></strong></p>\n" +
//                "<p>&nbsp;</p>\n" +
//                "<p style=\"text-align: left;\"><strong><em><span style=\"text-decoration: underline;\"><span style=\"text-decoration: line-through;\">这段左对齐</span></span></em></strong></p>\n" +
//                "<p style=\"text-align: center;\">居中w/o 所有格式</p>\n" +
//                "<p style=\"text-align: center;\">减少锁紧</p>\n" +
//                "<p style=\"text-align: right;\">右对齐</p>\n" +
//                "<p style=\"padding-left: 120px; text-align: left;\">&nbsp;</p>\n" +
//                "<p style=\"padding-left: 120px; text-align: left;\">左对齐才有缩进</p>\n" +
//                "<p style=\"padding-left: 60px; text-align: left;\">减少两个缩进比起上一个</p>\n" +
//                "<p style=\"padding-left: 60px; text-align: left;\"><img class=\"wscnph\" src=\"\" /></p>");
//        System.out.println(text);
//    }
}

